package org.geektimes.projects.third.login.controller;

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.geektimes.web.mvc.controller.Controller;
import org.geektimes.web.mvc.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.Response;
import java.io.IOException;

/**
 * Gitee Third Login
 * https://gitee.com/api/v5/oauth_doc#/list-item-1
 *
 * @author biwin
 */
@Path("/third")
public class ThirdGiteeController implements Controller {

    private final static String CLIENT_ID = "8f51775773dbc2207d6cf4b39730d48b330d8b4d6774ac4a471c7544c7ff8520";
    private final static String CLIENT_SECRET = "4778118f5f9f858cb37242d7fc21e14f967fa700adc618a74fd962b8db58d2ad";

    private final static String HOME_URL = "https://m30078.jp.cdjxt.net";
    /**
     * 请求 Code 的回调地址
      */
    private final static String CODE_CALLBACK_URL = HOME_URL + "/third/code";
    /**
     * 请求 Code Url
      */
    private final static String AUTHORIZATION_URL = "https://gitee.com/oauth/authorize?client_id={client_id" +
            "}&redirect_uri={redirect_uri}&response_type=code";

    /**
     * 授权 URL
      */
    private final static String GRANT_URL = "https://gitee.com/oauth/token";


    @GET
    @Path("/gitee")
    public void redirectPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String url = AUTHORIZATION_URL;
        url = url.replace("{client_id}", CLIENT_ID);
        url = url.replace("{redirect_uri}", CODE_CALLBACK_URL);
        response.sendRedirect(url);
    }

    @GET
    @Path("/code")
    public void grant(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String code = request.getParameter("code");
        System.out.println("code = " + code);
        if (StringUtils.isEmpty(code)) {
            throw new RuntimeException("获取 Code 异常!");
        }

        Client client = ClientBuilder.newClient();
        Form form = new Form();
        form.param("grant_type", "authorization_code");
        form.param("code", code);
        form.param("client_id", CLIENT_ID);
        form.param("redirect_uri", CODE_CALLBACK_URL);
        form.param("client_secret", CLIENT_SECRET);

        Entity<Form> entity = Entity.form(form);
        Response result = client.target(GRANT_URL).request().post(entity);
        String s = (String) result.getEntity();
        System.out.println("s = " + s);
        //{"access_token":"0f4563b6cf5213a18beb372b97e55e5c","token_type":"bearer","expires_in":86400,"refresh_token":"506a8036862277c9bf0df8a11aaa45e5757cc89d9b6bb252244998cafc51ce02","scope":"user_info","created_at":1618903530}
        response.sendRedirect("/");

    }

}
